﻿using System;
using System.Data;
using System.Collections.Generic;

using Clipper.Model;
using System.Collections.ObjectModel;

namespace Clipper.BLL
{
	/// <summary>
	/// SampleInfo
	/// </summary>
	public partial class SampleInfo
	{
		private readonly Clipper.DAL.SampleInfo dal=new Clipper.DAL.SampleInfo();
        DAL.PatientInfo mPatientDal = new DAL.PatientInfo();
        DAL.SampleDetectItems mSampleDetectItemDal = new DAL.SampleDetectItems();
        DAL.DetectItems mDetectItemsDal = new DAL.DetectItems();

		public SampleInfo()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long sampleID)
		{
			return dal.Exists(sampleID);
		}

		/// <summary>
		/// 增加一条数据 
		/// </summary>
		public bool Add(Clipper.Model.SampleInfo model)
		{
            bool res = false;
            if (model.PatientInfo.patientID != 0)
            {
                if (!mPatientDal.Exists(model.PatientInfo.patientID))//病人信息
                {
                    mPatientDal.Add(model.PatientInfo);
                }

                model.patientID = model.PatientInfo.patientID;
            }
			
            res= dal.Add(model);
            if (res)
            {
                if (model.DetectItems != null)//添加样本检测项
                {
                    foreach (Model.DetectItems item in model.DetectItems)
                    {
                        mSampleDetectItemDal.Add(new Model.SampleDetectItems()
                        {
                            DetectItem = item.detectItem,
                            SampleID = model.sampleID
                        });
                    }
                }
            }
            return res;
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.SampleInfo model)
		{
            bool res = false;
            if (model.PatientInfo.name != null)
            {
                if (!mPatientDal.Exists(model.PatientInfo.patientID))//病人信息
                {
                    mPatientDal.Update(model.PatientInfo);
                }
                model.patientID = model.PatientInfo.patientID;
            }

            res = dal.Update(model);
            if (res)
            {
                mSampleDetectItemDal.DeleteDetectItems(model.sampleID);//删除检测项
                    if (model.DetectItems != null)
                    {
                        foreach (Model.DetectItems item in model.DetectItems)
                        {
                            mSampleDetectItemDal.Add(new Model.SampleDetectItems()
                            {
                                DetectItem = item.detectItem,
                                SampleID = model.sampleID
                            });
                        }
                    }
            }
            return res;
		}

        /// <summary>
        /// 更新样本类型
        /// </summary>
        /// <returns></returns>
        public bool UpdateSampleType(int sampleId)
        {
            return false;
        }

		/// <summary>
		/// 删除一条数据,删除相关检测项
		/// </summary>
		public bool Delete(long sampleID)
		{
            mSampleDetectItemDal.DeleteDetectItems(sampleID);//删除检测项
			return dal.Delete(sampleID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string sampleIDlist )
		{
			return dal.DeleteList(sampleIDlist);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.SampleInfo GetModel(long sampleID)
		{
			
			return dal.GetModel(sampleID);
		}

		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.SampleInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.SampleInfo> DataTableToList(DataTable dt)
		{
			ObservableCollection<Clipper.Model.SampleInfo> modelList = new ObservableCollection<Clipper.Model.SampleInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Clipper.Model.SampleInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 获得数据列表,有病人信息
        /// </summary>
        public Clipper.Model.SampleInfo GetFullSampleInfo(long sampleID)
        {
            DataSet ds = dal.GetList("sampleID="+sampleID);
            Clipper.Model.SampleInfo model = null;
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        model.PatientInfo = mPatientDal.GetModel(model.patientID);
                        
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表,有病人信息
        /// </summary>
        public ObservableCollection<Clipper.Model.SampleInfo> GetFullSampleInfoList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            ObservableCollection<Clipper.Model.SampleInfo> modelList = new ObservableCollection<Clipper.Model.SampleInfo>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.SampleInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    //model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    long _sampleId = long.Parse(ds.Tables[0].Rows[n]["sampleID"].ToString());
                    model= GetSampleInfoAndItems(_sampleId);
                    if (model != null)
                    {
                        model.PatientInfo= mPatientDal.GetModel(model.patientID);
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得检查项
        /// </summary>
        /// <param name="sampleId"></param>
        /// <returns></returns>
        public ObservableCollection<Clipper.Model.DetectItems> GetSampleDetectItems(long sampleId)
        {
            DataSet dt= mSampleDetectItemDal.GetList("sampleID=" + sampleId);
            ObservableCollection<Clipper.Model.DetectItems> list = null;
            if (dt != null && dt.Tables[0].Rows.Count > 0)
            {
                list = new ObservableCollection<Model.DetectItems>();
                foreach (DataRow drow in dt.Tables[0].Rows)
                {
                    list.Add(mDetectItemsDal.GetModel(drow["DetectItem"].ToString()));
                }
            }
            return list;
        }
        /// <summary>
        /// 获得包含检测项，病人的实体
        /// </summary>
        /// <param name="sampleId"></param>
        /// <returns></returns>
        public Model.SampleInfo GetSampleInfoAndItems(long sampleID)
        {
            Model.SampleInfo model= dal.GetModel(sampleID);
            if (model != null)
            {
                model.PatientInfo = mPatientDal.GetModel(model.patientID);
                model.DetectItems = GetSampleDetectItems(sampleID);
            }
            return model;
        }
        /// <summary>
        /// 查询一段时间内输入的病人信息
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public ObservableCollection<Model.SampleInfo> GetSampleList(string from ,string to)
        {
            DataSet ds = dal.GetList(from,to);
            ObservableCollection<Clipper.Model.SampleInfo> modelList = new ObservableCollection<Clipper.Model.SampleInfo>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.SampleInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        model.PatientInfo = mPatientDal.GetModel(model.patientID);
                        if (model.PatientInfo.name != null)
                        {
                            modelList.Add(model);
                        }
                    }
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得新的样本号
        /// </summary>
        /// <returns></returns>
        public int GetLastSampleId()
        {
            return DAL.SQLiteHelper.GetMaxID("sampleID", "SampleInfo");
        }
        /// <summary>
        /// 获得试管中最后的ID号码
        /// </summary>
        /// <returns></returns>
        public int GetLastTubeSampleId()
        {
            return DAL.SQLiteHelper.GetMaxID("SampleID", "TestTubeInfo");
        }
        /// <summary>
        /// 添加样本的检测项
        /// </summary>
        /// <param name="sampleId">样本号</param>
        /// <param name="detectItem">检测项</param>
        /// <returns>操作是否成功</returns>
        public bool AddSampleItem(long sampleId,string detectItem)
        {
            bool res= mSampleDetectItemDal.Add(new Model.SampleDetectItems()
                {
                    DetectItem = detectItem,
                    SampleID = sampleId
                });
            return res;
        }

        public bool BatchAddSampleItem(List<Model.SampleDetectItems> models)
        {
            bool res = mSampleDetectItemDal.BatchAdd(models);
            return res;
        }
        /// <summary>
        /// 删除样本的检测项
        /// </summary>
        /// <param name="sampleId">样本号</param>
        /// <param name="detectItem">检测项</param>
        /// <returns>操作是否成功</returns>
        public bool DeleteSampleItem(long sampleId,string detectItem)
        {
            bool res = mSampleDetectItemDal.Delete(sampleId, detectItem);
            return res;
        }
        /// <summary>
        /// 删除样本所有的检测项
        /// </summary>
        /// <param name="sampleId"></param>
        /// <returns></returns>
        public bool DeleteAllSampleItem(long sampleId)
        {
           return  mSampleDetectItemDal.DeleteDetectItems(sampleId);//删除检测项
        }
		#endregion  ExtensionMethod
	}
}

